/**
 * Created by sunlf on 2017/7/21.
 * 登录api
 */
import Vue from 'vue'
import {loginUrl} from '../config/global.toml'

export default {
  /**
   * 登录函数
   * @param _data 传递的登录数据
   * @returns {Promise.<TResult>|*}
   */
  remoteLogin(_data) {
    return new Promise((resolve, reject) => {
      Vue.axios.request({
        method: 'POST',
        url: loginUrl,
        transformRequest: [(data) => {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: _data
      }).then((res) => {
        resolve({code: 0, data: res.data})
      }).catch(err => {
        resolve({code: 1, data: err})
      })
    })
  }
}
